<html>
    <head>
        <meta charset="utf-8"/>
        <title>
        </title>
    </head>
    <body>
        <div style="text-align: center;">
            <textarea id="textarea" style="width: 600px;height: 250px;" placeholder="请输入json"></textarea>
        </div>
        <div style="text-align: center;">
            <button onclick="start()" style="width: 100px;height: 30px">转oc</button>
        </div>
        <div id="result" style="margin: 0 auto;width: 600px;font-size: 22px"></div>
    </body>
    <script type="text/javascript">
        var gettype=Object.prototype.toString;
        String.prototype.firstUpperCase = function(){
            return this.replace(/\b(\w)(\w*)/g, function($0, $1, $2) {
                                return $1.toUpperCase() + $2.toLowerCase();
                                });
        }
    function start(){
        try{
            var result = JSON.parse(document.getElementById("textarea").value);
        }catch(error){
            console.log("解析JSON文件失败:"+error);
            return;
        }
        if (!result) {
            console.log("解析JSON文件无效");
            return;
        }
        document.classArray = new Array();
        parseObject("MyObject",result);
        
        var stringAll="";
        for (var i = document.classArray.length-1; i >=0 ; i--) {
            let cla = document.classArray[i];
            stringAll+=" \r\n\r\n@interface "+cla.name+" : NSObject\r\n\r\n";
            console.log(" \r\n\r\n@interface "+cla.name+" : NSObject\r\n\r\n");
            for (var j = 0; j < cla.property.length; j++) {
                stringAll+=cla.property[j]+"\r\n\r\n";
                console.log(cla.property[j]+"\r\n\r\n");
            }
            stringAll+="@end\r\n\r\n";
            console.log("@end\r\n\r\n");
        }
        stringAll+="===========m文件======================\r\n";
        console.log("===========m文件======================\r\n");
        for (var i = document.classArray.length-1; i >=0 ; i--) {
            let cla = document.classArray[i];
            stringAll+="@implementation "+cla.name+"\r\n\r\n@end\r\n\r\n";
            console.log("@implementation "+cla.name+"\r\n\r\n@end\r\n\r\n");
        }
        document.getElementById("result").innerText = stringAll;
        
    }
    
    
    function parseObject(k,result){
        let c = new Class(k);
        document.classArray.push(c);
        for (var i = 0; i < Object.getOwnPropertyNames(result).length; i++) {
            let key = Object.getOwnPropertyNames(result)[i];
            let value = result[key];
            let type = getType(value);
            if(type==null){
                continue;
            }
            if (type=="Object") {
                //进行二次解析
                if (Object.getOwnPropertyNames(value).length==0) {
                    c.property.push("@property(nonatomic, strong) NSDictionary *"+key+";");
                }else{
                    parseObject(key.firstUpperCase(),value);
                    c.property.push("@property(nonatomic, strong) "+key.firstUpperCase()+" *"+key+";");
                }
                continue;
            }
            if (type=="Array") {
                if (value.length>0) {
                    let obj = value[0];
                    let t = getType(obj);
                    if (t==null) {
                        continue;
                    }
                    if (t=="Object") {
                        c.property.push("@property(nonatomic, strong) NSArray"+" *"+key+";");
                        parseObject(key.firstUpperCase(),obj);
                    }else{
                        c.property.push("@property(nonatomic, strong) NSArray<"+t+"*>*"+key+";");
                    }
                }else{
                    c.property.push("@property(nonatomic, strong) NSArray *"+key+";");
                }
                continue;
            }
            if (type=="id") {
                c.property.push("@property(nonatomic, strong) "+type+""+key+";");
                continue;
            }
            c.property.push("@property(nonatomic, strong) "+type+" *"+key+";");
        }
    }
    
    
    
    function getType(obj){
        if (typeof obj == 'number') {
            return "NSNumber";
        }
        if (typeof obj == 'undefined') {
            return "id";
        }
        if (typeof obj == 'null') {
            return "id";
        }
        if (typeof obj == 'function') {
            return null;
        }
        if (typeof obj == 'string') {
            return "NSString"
        }
        if (typeof obj == 'boolean') {
            return "NSNumber"
        }
        if (typeof obj == 'object') {
            if (gettype.call(obj)=="[object Object]") {
                return "Object";
            }
            if (gettype.call(obj)=="[object Array]") {
                return "Array";
            }
            if (gettype.call(obj)=="[object Null]"){
                return "id";
            }
        }
    }
    
    function Class(name){
        this.name = name;
        this.property = new Array();
    }
    </script>
</html>
————————————————
版权声明：本文为CSDN博主「林大帅6688」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/weixin_41732253/article/details/104105255
